package com.yijiaganxi.services.web.category;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yijiaganxi.common.entity.category.YjgxCategory;
import org.yijiaganxi.common.res.R;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.yijiaganxi.common.GlobalConstants;
import com.yijiaganxi.services.service.category.YjgxCategoryService;
import java.util.Date;
import javax.validation.constraints.NotBlank;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;

/**
 * <p>
 * 分类管理 前端控制器
 * </p>
 *
 * @author zhangguowei123
 * @since 2020-08-29
 */
@Validated
@RestController
@RequestMapping("/services/yjgxCategory/"+GlobalConstants.WEB_FLAG)
public class YjgxCategoryController {

	@Autowired
	public YjgxCategoryService  categoryService;
	
	/**
	 * 查询所有分类
	 * @return
	 */
	@GetMapping("selectAllCategory")
	public R selectAllCategory() {
		return R.opertionListResponse( categoryService.selectList(new EntityWrapper<YjgxCategory>()));
	}
	
	
	/**
	 * 根据条件查询分类
	 * @return
	 */
	@GetMapping("getCategoryByCondition")
	public Object getCategoryByCondition(String name,Integer status) {
		EntityWrapper<YjgxCategory> entityWrapper = new EntityWrapper<YjgxCategory>();
		if(StringUtils.isNotBlank(name))
			entityWrapper.eq("name", name);
		if(status !=null)
			entityWrapper.eq("status", status);
		
		return R.opertionListResponse( categoryService.selectList(entityWrapper));
	}
	
	/**
	 * 添加分类
	 * @param banner
	 * @return
	 */
	@PostMapping("addCategory")
	public R addCategory(@Validated YjgxCategory category) {
		return R.insertOrUpdateResponse(categoryService.insert(category));
	}
	
	
	/**
	 * 更新分类
	 * @param banner
	 * @return
	 */
	@PostMapping("updateCategory")
	public R updateCategory(YjgxCategory category) {
		if(category.getId()==null)
			return R.error("Id 更新操作必填");
		category.setModifyDate(new Date());
		return R.insertOrUpdateResponse(categoryService.update(category, new EntityWrapper<YjgxCategory>()
				.eq("id", category.getId())));
	}
	
	/**
	 * 删除分类
	 * @param id
	 * @return
	 */
	@PostMapping("deleteCategory")
	public R deleteCategory(@NotBlank(message = "id不能为空") String id) {
		return R.insertOrUpdateResponse(categoryService.deleteById(id));
	}
}

